package com.art.base.util;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.Signature;
import android.os.Environment;
import android.text.format.Formatter;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateEncodingException;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;

/**
 * 文件工具类
 * 
 * @author dongtianhao 2016年8月19日17:04:11
 */
public class FileUtils {

	/**
	 * 格式转换文件大小
	 * 
	 * @param context
	 * @param fileSize
	 * @return
	 */
	@SuppressLint("NewApi")
	public static String formatFileSize(Context context, long fileSize) {
		return Formatter.formatFileSize(context, fileSize);
	}

	/**
	 * 判断该文件是否存在
	 * 
	 * @return
	 */
	public static boolean isExist(String path) {
		try {
			File f = new File(path);
			if (f.exists()) {
				return true;
			}
		} catch (Exception e) {
			return false;
		}
		return false;

	}
	/**
	 * 保存异常信息到文件
	 * @param data
	 * @param file_name
	 */
	public static void saveFile(String data, String file_name) {
		File sdPath = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
		if (!sdPath.exists()) {
			sdPath.mkdirs();
		}
		File file = new File(sdPath, file_name);
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(file);
			fos.write(data.getBytes("UTF-8"));
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}


	//这个是获取SHA1的方法
	public static String getCertificateSHA1Fingerprint(Context context) {
		//获取包管理器
		PackageManager pm = context.getPackageManager();
		//获取当前要获取SHA1值的包名，也可以用其他的包名，但需要注意，
		//在用其他包名的前提是，此方法传递的参数Context应该是对应包的上下文。
		String packageName = context.getPackageName();
		//返回包括在包中的签名信息
		int flags = PackageManager.GET_SIGNATURES;
		PackageInfo packageInfo = null;
		try {
			//获得包的所有内容信息类
			packageInfo = pm.getPackageInfo(packageName, flags);
		} catch (PackageManager.NameNotFoundException e) {
			e.printStackTrace();
		}
		//签名信息
		Signature[] signatures = packageInfo.signatures;
		byte[] cert = signatures[0].toByteArray();
		//将签名转换为字节数组流
		InputStream input = new ByteArrayInputStream(cert);
		//证书工厂类，这个类实现了出厂合格证算法的功能
		CertificateFactory cf = null;
		try {
			cf = CertificateFactory.getInstance("X509");
		} catch (CertificateException e) {
			e.printStackTrace();
		}
		//X509证书，X.509是一种非常通用的证书格式
		X509Certificate c = null;
		try {
			c = (X509Certificate) cf.generateCertificate(input);
		} catch (CertificateException e) {
			e.printStackTrace();
		}
		String hexString = null;
		try {
			//加密算法的类，这里的参数可以使MD4,MD5等加密算法
			MessageDigest md = MessageDigest.getInstance("SHA1");
			//获得公钥
			byte[] publicKey = md.digest(c.getEncoded());
			//字节到十六进制的格式转换
			hexString = byte2HexFormatted(publicKey);
		} catch (NoSuchAlgorithmException e1) {
			e1.printStackTrace();
		} catch (CertificateEncodingException e) {
			e.printStackTrace();
		}
		return hexString;
	}
	//这里是将获取到得编码进行16进制转换
	private static String byte2HexFormatted(byte[] arr) {
		StringBuilder str = new StringBuilder(arr.length * 2);
		for (int i = 0; i < arr.length; i++) {
			String h = Integer.toHexString(arr[i]);
			int l = h.length();
			if (l == 1)
				h = "0" + h;
			if (l > 2)
				h = h.substring(l - 2, l);
			str.append(h.toUpperCase());
			if (i < (arr.length - 1))
				str.append(':');
		}
		return str.toString();
	}
}
